package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.io.*;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import model.Photo;
/*
 * This is the main class. Your assigment should pretty much be done in here.
 * This application is a simple image viewer. It shows the images at the top
 * part and has a small control panel at the bottom that allows you to cycle
 * through the various pictures.
 *
 * Good luck with your assignment!
 */
public class OOP_PO2_Main extends JFrame{
    private JPanel mainPanel;
    private MainView mainView;
    private ControlPanelView controlView;
    private List<String> locaties;
    private String LocationFile;
    private List<Photo> photolist;
    private int photoID = 0;

    public OOP_PO2_Main(){
        initComponents();     
        
        LocationFile = "output/locaties.txt";
        //writeLocations();
        readLocations();
        photolist = getPhotos();
        photolistToView();
        
        //WriteFile();
        //
        // for C:
        // - save images to file "locaties.txt" (make a method and call it)
        // - load images from file "locaties.txt" (make a method and call it)
        //
        // for D:
        // - save an image to a binary file "foto.ser" (make a method and call it)
        writePhoto(photolist.get(0));
        readPhoto();
    }

    // <editor-fold defaultstate="collapsed" desc="Component initialization">
    /*
     * Initialization of components. Don't touch this.
     */
    public void initComponents(){
        setPreferredSize(new Dimension(320, 400));

        mainPanel = new JPanel();
        mainView = new MainView();
        controlView = new ControlPanelView(mainView);
        mainPanel.setLayout(new BorderLayout());

        add(mainPanel);
        mainPanel.add(mainView, BorderLayout.CENTER);
        mainPanel.add(controlView, BorderLayout.SOUTH);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        pack();
    }
    // </editor-fold>

    /*
     * TODO OOP_PO2_B2: Use this method to read all images from the disk
     * and create a Photo object for every image. Add all Photo objects
     * to an ArrayList.
     */
    public List getPhotos(){
        List photos = new ArrayList<Photo>();
        for(String locatie : locaties) {
            Image image = getImage(locatie);
            photos.add(new Photo(photoID, locatie, image));
            photoID++;
        }
        return photos;
    }

    /*
     * TODO OOP_PO2_B1: Copy-paste the code from assignment A here.
     */
    public Image getImage(String path){
        ImageIcon plaatje = new ImageIcon(path);
        System.out.println("getImageLoadStatus:"+plaatje.getImageLoadStatus());
        Image image = plaatje.getImage();
        return image;
    }
    /**
     * Voegt de lijst met Photo objecten toe aan de GUI
     */
    public void photolistToView(){
        for(Photo photo : photolist){
            ImageView imageView = new ImageView(photo);
            mainView.add("foto"+photo.getId(), imageView);
        }
    }
    /**
     * Voeg een enkele foto toe aan de GUI
     * @param newphoto De foto die wordt toegevoegd
     */
    public void insertPhotoInView(Photo newphoto){
        photolist.add(newphoto);
        ImageView imageView = new ImageView(newphoto);
        mainView.add("foto"+newphoto.getId(), imageView);
    }
    
    public void writeFile() {
        try {
            FileWriter outfile = new FileWriter("output/locaties.txt");
            PrintWriter out = new PrintWriter(outfile);
            List photos = new ArrayList<Photo>();
            for(String locatie : locaties) {
                out.print(locatie);
            }
            out.close();
        } catch(IOException e) {
            System.out.println("FOUTFOUTFOUT");
        }
    }
    /**
     * Slaat de foto op als geserialiseerd object.
     * @param photo De foto om op te slaan
     * @param path Locatie van het bestand dat je opslaat
     */
    public void writePhoto(Photo photo, String path){
        FileOutputStream fo;
        ObjectOutputStream oos;
        
        try{
        fo = new FileOutputStream(path);
        oos = new ObjectOutputStream(fo);
        
        oos.writeObject(photo);
        
        oos.close();
        fo.close();
        } catch (IOException ioexc){
            ioexc.printStackTrace();
        }        
    }
    /**
     * Slaat de foto op als geserialiseerd object als output/foto.ser
     * @param photo De foto om op te slaan
     */
    public void writePhoto(Photo photo){
        writePhoto(photo, "output/foto.ser");
    }
    /**
     * Leest de foto uit het gegeven bestand en importeert hem in de GUI
     * @param path Het pad naar het te openen bestand
     */
    public void readPhoto(String path){
        FileInputStream fi;
        ObjectInputStream ois;
        Photo readPhoto;
        
        try{
            fi = new FileInputStream(path);
            ois = new ObjectInputStream(fi);
            
            readPhoto = (Photo) ois.readObject();
            insertPhotoInView(readPhoto);
            
            ois.close();
            fi.close();
        }catch(FileNotFoundException exc){
            exc.printStackTrace();
        }catch(IOException exc){
            exc.printStackTrace();
        }catch(ClassNotFoundException exc){
            System.out.println("Class: "+exc.getClass()+" not found");
            exc.printStackTrace();
        }
    }
    /**
     * Leest de foto uit output/foto.ser en importeert hem in de GUI
     */
    public void readPhoto(){
        readPhoto("output/foto.ser");
    }

    /**
     * This function reads the location text file line for line and adds the lines in an arraylist.
     */
    public void readLocations() {
        try{
            FileInputStream fstream = new FileInputStream(LocationFile);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            locaties = new ArrayList<String>();
            while ((strLine = br.readLine()) != null) {
                locaties.add(strLine);
            }
            in.close();
        }catch (Exception e){
            System.err.println("Error: " + e.getMessage());
        }
    }
    
    /**
     * This function reads the arraylist locaties and adds these lines in the location text file.
     */
    public void writeLocations() {
        try {
            FileWriter outfile = new FileWriter(LocationFile);
            PrintWriter out = new PrintWriter(outfile);
            for(String locatie : locaties) {
                out.println(locatie);
            }
            out.close();
        } catch(IOException e) {
            System.out.println("FOUTFOUTFOUT");
            e.printStackTrace();
        }
    }
    
    public static void main(String args[]){
        new OOP_PO2_Main(); 
    }
    
}
